package org.xiyinjihe.platform.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xiyinjihe.platform.dao.UserMapper;
import org.xiyinjihe.platform.dao.UserTokenMapper;
import org.xiyinjihe.platform.dao.VisitMapper;
import org.xiyinjihe.platform.po.*;
import org.xiyinjihe.platform.service.UserService;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserTokenMapper userTokenMapper;

    @Autowired
    VisitMapper visitMapper;


    @Override
    public User saveUserInfo(WxUserInfo userInfo) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andOpenIdEqualTo(userInfo.getOpenId());
        List<User> users = userMapper.selectByExample(example);
        if(users == null || users.size() ==0){
            User newUser = new User();
            newUser.setOpenId(userInfo.getOpenId());
            newUser.setNickname(userInfo.getNickName());
            newUser.setGender(userInfo.getGender());
            newUser.setCountry(userInfo.getCountry());
            newUser.setProvince(userInfo.getProvince());
            newUser.setCity(userInfo.getCity());
            newUser.setAvatarurl(userInfo.getAvatarUrl());
            newUser.setUnionid(userInfo.getUnionId());
            int insert = userMapper.insert(newUser);
            if(insert>0)
                return newUser;
            else
                return null;
        }else{
            return users.get(0);
        }

    }

    @Override
    public void saveUserToken(Long userId, String token) {
        UserTokenExample example = new UserTokenExample();
        UserTokenExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<UserToken> userTokens = userTokenMapper.selectByExample(example);
        if(userTokens == null || userTokens.size() ==0){
            UserToken newUserToken = new UserToken();
            newUserToken.setUserId(userId);
            newUserToken.setToken(token);
            newUserToken.setGenerateTime(new Date());
            userTokenMapper.insert(newUserToken);
        }else{
            UserToken userToken = userTokens.get(0);
            userToken.setToken(token);
            userToken.setGenerateTime(new Date());
            userTokenMapper.updateByPrimaryKeySelective(userToken);
        }
    }

    @Override
    public boolean isTokenValidate(String token) {
        if(token == null || token.isEmpty()){
            return false;
        }
        UserTokenExample example = new UserTokenExample();
        UserTokenExample.Criteria criteria = example.createCriteria();
        criteria.andTokenEqualTo(token);
        List<UserToken> userTokens = userTokenMapper.selectByExample(example);
        if(userTokens == null || userTokens.size() <=0){
            return false;
        }else{
            UserToken userToken = userTokens.get(0);
            Date generateTime = userToken.getGenerateTime();
            Date now = new Date();
            long diffInMillies = Math.abs(now.getTime() - generateTime.getTime());
            long diff = TimeUnit.HOURS.convert(diffInMillies, TimeUnit.MILLISECONDS);
            if(diff>2){
                return false;
            }
        }
        return true;
    }

    @Override
    public void setUserInfo(long userId, String name, String buddhistName, Boolean hasAsk) {
        User user = userMapper.selectByPrimaryKey(userId);
        if(user != null){
            if(name != null) {
                user.setName(name);
            }
            if(buddhistName != null) {
                user.setBuddhistName(buddhistName);
            }
            if(hasAsk!= null) {
                user.setHasAsk(hasAsk ? "1" : "0");
            }
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public User getUserInfo(long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public long saveVisit(VisitWithBLOBs visit) {
        return visitMapper.insert(visit);
}
}
